package com.cityeye.mmsentity;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.cityeye.dao.PatrolDao;
import com.cityeye.entity.Mmessage;
import com.cityeye.entity.Patrol;
import com.cityeye.util.CityUtil;

/**
 * MsgInbox entity. @author MyEclipse Persistence Tools
 */

public class MsgInbox implements java.io.Serializable {
	// Fields
	public static final java.text.SimpleDateFormat noFormater = new java.text.SimpleDateFormat(
	"yyMMdd");
	public static final int TYPE_TXT = 0;
	public static final int TYPE_MMS = 1;

	public static final int DOWN_STAT_PENDING = 0;
	public static final int DOWN_STAT_DOING = 1;
	public static final int DOWN_STAT_READY = 100;
	public static final int DOWN_STAT_FAIL = -100;

	// Fields

	private Integer id;
	private String sender;
	private Integer msgType;
	private String msgTitle;
	private String mmsurl;
	private String mmscontentLocation;
	private Timestamp msgArrivedTime;
	private Timestamp mmsdownloadedTime;
	private Integer mmsdownloadedState;
	private Integer commPort;

	// Constructors

	/** default constructor */
	public MsgInbox() {
	}

	/** minimal constructor */
	public MsgInbox(String sender, Integer msgType, Timestamp msgArrivedTime,
			Integer mmsdownloadedState, Integer commPort) {
		this.sender = sender;
		this.msgType = msgType;
		this.msgArrivedTime = msgArrivedTime;
		this.mmsdownloadedState = mmsdownloadedState;
		this.commPort = commPort;
	}

	/** full constructor */
	public MsgInbox(String sender, Integer msgType, String msgTitle,
			String mmsurl, String mmscontentLocation, Timestamp msgArrivedTime,
			Timestamp mmsdownloadedTime, Integer mmsdownloadedState,
			Integer commPort) {
		this.sender = sender;
		this.msgType = msgType;
		this.msgTitle = msgTitle;
		this.mmsurl = mmsurl;
		this.mmscontentLocation = mmscontentLocation;
		this.msgArrivedTime = msgArrivedTime;
		this.mmsdownloadedTime = mmsdownloadedTime;
		this.mmsdownloadedState = mmsdownloadedState;
		this.commPort = commPort;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getSender() {
		return this.sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public Integer getMsgType() {
		return this.msgType;
	}

	public void setMsgType(Integer msgType) {
		this.msgType = msgType;
	}

	public String getMsgTitle() {
		return this.msgTitle;
	}

	public void setMsgTitle(String msgTitle) {
		this.msgTitle = msgTitle;
	}

	public String getMmsurl() {
		return this.mmsurl;
	}

	public void setMmsurl(String mmsurl) {
		this.mmsurl = mmsurl;
	}

	public String getMmscontentLocation() {
		return this.mmscontentLocation;
	}

	public void setMmscontentLocation(String mmscontentLocation) {
		this.mmscontentLocation = mmscontentLocation;
	}

	public Timestamp getMsgArrivedTime() {
		return this.msgArrivedTime;
	}

	public void setMsgArrivedTime(Timestamp msgArrivedTime) {
		this.msgArrivedTime = msgArrivedTime;
	}

	public Timestamp getMmsdownloadedTime() {
		return this.mmsdownloadedTime;
	}

	public void setMmsdownloadedTime(Timestamp mmsdownloadedTime) {
		this.mmsdownloadedTime = mmsdownloadedTime;
	}

	public Integer getMmsdownloadedState() {
		return this.mmsdownloadedState;
	}

	public void setMmsdownloadedState(Integer mmsdownloadedState) {
		this.mmsdownloadedState = mmsdownloadedState;
	}

	public Integer getCommPort() {
		return this.commPort;
	}

	public void setCommPort(Integer commPort) {
		this.commPort = commPort;
	}

	public Mmessage toMmessage(int msgIndex) {
		Mmessage msg = new Mmessage();
		msg.setId(null);
		msg.setArrivetime(getMsgArrivedTime());

		if(getMsgArrivedTime()!=null){
			String noStr = noFormater.format(getMsgArrivedTime());
			String indexStr = String.format("%04d", msgIndex);  
				
			msg.setNo(noStr+indexStr);
		}
		
		if (getMsgTitle().trim() != null)
			msg.setMsgtitle(getMsgTitle().trim());
		msg.setMsgtype(getMsgType());

		String mmsFiles = getMmscontentLocation();
		List<String> imgs= new ArrayList<String>();		
		if (mmsFiles != null && mmsFiles.length() > 0) {
			String[] files = mmsFiles.split(Mmessage.FILE_SPERATOR);

			for (String file : files) {
				file = file.trim();
				if (file.length() > 0) {
					String lowerPath = file.toLowerCase();
					if (lowerPath.endsWith(".txt")) {
						String fileText = CityUtil.readTextFile(file);
						if (msg.getMsgtitle().length() > 0) {
							msg.setMsgtitle(msg.getMsgtitle() + "\n  "
									+ fileText);
						}else{
							msg.setMsgtitle(fileText);							
						}
					} else if (lowerPath.endsWith(".jpg")
							|| lowerPath.endsWith(".jpeg")
							|| lowerPath.endsWith(".png")
							|| lowerPath.endsWith(".bmp")
							|| lowerPath.endsWith(".gif")) {
						imgs.add(file);
					}
				}
			}		
		}
		msg.setMMSImages(imgs);
		
		msg.setSender(getSender());		
		if(msg.getSender()!=null && msg.getSender().length()>6)
		{
			String phoneNo = msg.getSender();
			// 11 digit mobile phone
			if(phoneNo.length()>11){
				phoneNo = phoneNo.substring(phoneNo.length()-11,phoneNo.length());
			}
			
			PatrolDao patrolDao = new PatrolDao();
			List<Patrol> pList = patrolDao.findByProperty("mobile", phoneNo);
			List<Patrol> p2List = patrolDao.findByProperty("mobile2", phoneNo);	
			pList.addAll(p2List);
			if(pList.size()>0){
				msg.setPatrol(pList.get(0));
			}
		}
		
		return msg;
	}
}